package com.app.config;

import com.app.service.JsonService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration // 标识此类是配置类
@ConditionalOnClass(JsonService.class) // 表示只有指定的class在classpath上时才能被注册
@EnableConfigurationProperties(JsonProperties.class) // 激活@ConfigurationProperties
public class JsonConfiguration {

    private JsonProperties jsonProperties;

    public JsonConfiguration(JsonProperties jsonProperties) {
        this.jsonProperties = jsonProperties;
    }

    @Bean
    @ConditionalOnMissingBean(JsonService.class) // 当容器没有此bean时，才注册
    public JsonService jsonService() {
        JsonService jsonService =  new JsonService();
        jsonService.setPrefixName(jsonProperties.getPrefixName());
        jsonService.setSuffixName(jsonProperties.getSuffixName());
        return jsonService;
    }
}
